package com.blank.web.servlet;

import com.blank.web.service.ClassService;
import com.blank.web.service.StudentService;
import com.mchange.v2.c3p0.util.TestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "delClassServlet",urlPatterns = "/delClass")
public class delClassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码方式,解决乱码问题
        response.setContentType("text/html;charset=utf-8");

        //获取request中的cid参数
        int cid = Integer.parseInt(request.getParameter("cid"));
        System.out.println(cid);
        ClassService classService = new ClassService();
        int i = classService.judgeContainStudent(cid);
        System.out.println(i);
        //获取输出流

        PrintWriter out = response.getWriter();
        /*String content = "window.location.href='index.jsp'";*/

        String contextPath = this.getServletContext().getContextPath();

        String hrefPath = contextPath + "/showClassInfo";


        if(i != 0){
            String content = "alert('删除失败,该班级有学生在呢！');location.href='"+hrefPath+"'";
            out.println("<script language=\"javascript\">" + content + "</script>");
            out.flush();
            out.close();

        }else{
            classService.DelClass(cid);
            String content = "alert('删除成功');location.href='"+hrefPath+"'";
            out.println("<script language=\"javascript\">" + content + "</script>");
            out.flush();
            out.close();

        }
        request.getRequestDispatcher("showClassInfo").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
